#!/bin/bash -e

function info() {
echo Usage: `basename $0` '[-o(port)] run_path'
exit 2
}

args=$*
while getopts  ":p:f:o:" opt; do
	case  $opt  in
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
        o) port=$OPTARG;;
		*) info;;
	esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

port=${port:=8090}
label=web_code
log=/mnt/ilustre/app/medical/.pub/.mnt/web_code.$port.`id -u`.log
tools_path=${tools_path:=/mnt/ilustre/app/medical/tools/bk}

if
    ps -ef|grep $label|grep python|grep -F `whoami`|grep $port
    then
        cat $log
else

    # . $var

    web_code_zip=$tools_path/web_code.zip
    if test ! -e "$web_code_zip"; then echo web code not found.; exit 1; fi
    
    
    run_path=`path_abs.sh $1`
    cd $run_path
    dir_new=$label.`date +%m%d%y`
    unzip $web_code_zip && rename $label $dir_new $label
    
    cd $dir_new
    nohup ./$label.py $port >>.log 2>&1 & >>.log && echo $run_path > $log && $0 $args
    
fi
